package com.yuexunit.mobilenurse.module.NursingDocuments.ui;

import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.yuexunit.mobilenurse.R;
import com.yuexunit.mobilenurse.config.AppConfig;
import com.yuexunit.mobilenurse.module.NursingDocuments.model.impl.DocumentRecordDetailModel;
import com.yuexunit.mobilenurse.module.NursingDocuments.presenter.impl.DocumentRecordDetailPresenter;
import com.yuexunit.mobilenurse.module.NursingDocuments.ui.view.IDocumentRecordDetailView;

import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.utils.PreferenceHelper;
import org.kymjs.kjframe.utils.StringUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2016/12/30.
 */
public class Act_DocumentPrenatalDetail extends KJActivity implements IDocumentRecordDetailView {

    @Bind(R.id.signinput_name)
    TextView signinputName;
    @Bind(R.id.signinput_bedno)
    TextView signinputBedno;
    @Bind(R.id.signinput_visitno)
    TextView signinputVisitno;
    @Bind(R.id.documentprenatal_v_edt)
    EditText documentprenatalVEdt;
    @Bind(R.id.documentprenatal_bp_01)
    EditText documentprenatalBp01;
    @Bind(R.id.documentprenatal_bp_02)
    EditText documentprenatalBp02;
    @Bind(R.id.documentprenatal_rupture_edit)
    Spinner documentprenatalRuptureEdit;
    @Bind(R.id.documentprenatal_fetalmovement_edit)
    EditText documentprenatalFetalmovementEdit;
    @Bind(R.id.documentprenatal_urine_edit)
    EditText documentprenatalUrineEdit;
    @Bind(R.id.documentprenatal_intrapartum_edit)
    EditText documentprenatalIntrapartumEdit;
    @Bind(R.id.documentprenatal_knee_edit)
    EditText documentprenatalKneeEdit;
    @Bind(R.id.documentprenatal_prom_edit)
    Spinner documentprenatalPromEdit;
    @Bind(R.id.documentprenatal_pain_edit)
    EditText documentprenatalPainEdit;
    @Bind(R.id.documentprenatal_contractions_edit)
    Spinner documentprenatalContractionsEdit;
    @Bind(R.id.documentprenatal_contractionsintensity_edit)
    EditText documentprenatalContractionsintensityEdit;
    @Bind(R.id.documentprenatal_breath_edit)
    EditText documentprenatalBreathEdit;
    @Bind(R.id.documentprenatal_sleep_edit)
    Spinner documentprenatalSleepEdit;
    @Bind(R.id.documentprenatal_womb_edit)
    EditText documentprenatalWombEdit;

    public String inpNo;//住院号
    public String recorder;//记录人员id

    private DocumentRecordDetailPresenter presenter;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_documentprenataldetail);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        presenter = new DocumentRecordDetailPresenter(new DocumentRecordDetailModel(), this);
        inpNo = PreferenceHelper.readString(aty, AppConfig.CURRENT_PATIENT_INFO, AppConfig.CURRENT_PATIENT_VISITNO);
        recorder = PreferenceHelper.readString(aty, AppConfig.NURSE_INFO, AppConfig.NURSE_LOGID);
    }

    @Override
    public void initWidget() {
        super.initWidget();
        signinputName.setText(PreferenceHelper.readString(aty, AppConfig.CURRENT_PATIENT_INFO, AppConfig.CURRENT_PATIENT_NAME));
        signinputBedno.setText(PreferenceHelper.readString(aty, AppConfig.CURRENT_PATIENT_INFO, AppConfig.CURRENT_PATIENT_BEDCODE));
        signinputVisitno.setText(inpNo);
    }

    @OnClick({R.id.titlebar_img_back, R.id.right_text, R.id.titlebar_img_menu})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.titlebar_img_back:
                finish();
                break;
            case R.id.right_text:
                buildValue();
                break;
            case R.id.titlebar_img_menu:
                buildValue();
                break;
        }
    }

    private void buildValue() {
        StringBuilder builder = new StringBuilder();
        if (!StringUtils.isEmpty(documentprenatalVEdt.getText().toString())) {
            builder.append("723" + ":" + documentprenatalVEdt.getText().toString());
        }

        if (!(StringUtils.isEmpty(documentprenatalBp01.getText()) && StringUtils.isEmpty(documentprenatalBp02.getText()))) {
            if (builder.length() != 0) {
                builder.append(",725:" + documentprenatalBp01.getText() + "/" + documentprenatalBp02.getText());
            } else {
                builder.append("725:" + documentprenatalBp01.getText() + "/" + documentprenatalBp02.getText());
            }
        }

        if (!StringUtils.isEmpty(documentprenatalRuptureEdit.getSelectedItem().toString())) {
            if (builder.length() != 0) {
                builder.append(",727:" + documentprenatalRuptureEdit.getSelectedItem().toString().substring(0, 1));
            } else {
                builder.append("727:" + documentprenatalRuptureEdit.getSelectedItem().toString().substring(0, 1));
            }
        }

        if (!StringUtils.isEmpty(documentprenatalFetalmovementEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",728:" + documentprenatalFetalmovementEdit.getText() );
            } else {
                builder.append("728:" + documentprenatalFetalmovementEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentprenatalUrineEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",729:" + documentprenatalUrineEdit.getText() );
            } else {
                builder.append("729:" + documentprenatalUrineEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentprenatalIntrapartumEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",730:" + documentprenatalIntrapartumEdit.getText() );
            } else {
                builder.append("730:" + documentprenatalIntrapartumEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentprenatalKneeEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",731:" + documentprenatalKneeEdit.getText() );
            } else {
                builder.append("731:" + documentprenatalKneeEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentprenatalPromEdit.getSelectedItem().toString())) {
            if (builder.length() != 0) {
                builder.append(",732:" + documentprenatalPromEdit.getSelectedItem().toString().substring(0, 1));
            } else {
                builder.append("732:" + documentprenatalPromEdit.getSelectedItem().toString().substring(0, 1));
            }
        }

        if (!StringUtils.isEmpty(documentprenatalPainEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",733:" + documentprenatalPainEdit.getText() );
            } else {
                builder.append("733:" + documentprenatalPainEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentprenatalContractionsEdit.getSelectedItem().toString())) {
            if (builder.length() != 0) {
                builder.append(",735:" + documentprenatalContractionsEdit.getSelectedItem().toString().substring(0, 1));
            } else {
                builder.append("735:" + documentprenatalContractionsEdit.getSelectedItem().toString().substring(0, 1));
            }
        }

        if (!StringUtils.isEmpty(documentprenatalContractionsintensityEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",745:" + documentprenatalContractionsintensityEdit.getText() );
            } else {
                builder.append("745:" + documentprenatalContractionsintensityEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentprenatalBreathEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",746:" + documentprenatalBreathEdit.getText() );
            } else {
                builder.append("746:" + documentprenatalBreathEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentprenatalSleepEdit.getSelectedItem().toString())) {
            if (builder.length() != 0) {
                builder.append(",748:" + documentprenatalSleepEdit.getSelectedItem().toString().substring(0, 1));
            } else {
                builder.append("748:" + documentprenatalSleepEdit.getSelectedItem().toString().substring(0, 1));
            }
        }

        if (!StringUtils.isEmpty(documentprenatalWombEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",749:" + documentprenatalWombEdit.getText() );
            } else {
                builder.append("749:" + documentprenatalWombEdit.getText());
            }
        }

        Map<String, String> params = new HashMap<>();
        params.put("inpNo", inpNo);
        params.put("timestamp", System.currentTimeMillis() + "");
        params.put("timepoint", System.currentTimeMillis() + "");
        params.put("templateId", "35");
        params.put("signData", builder.toString());
        params.put("recorder", recorder);
        presenter.uploadRecord(params);

    }

    @Override
    public void uploadRecord() {
        finish();
    }
}
